package com.seari.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seari.bean.DeviceOfServer;
import com.seari.bean.DeviceOfSwitch;
import com.seari.pojo.MessageBean;
import com.seari.service.MonitorService;
import com.seari.utils.StringHelper;

/**
 * @author Rainbow
 *	服务器与交换机资源使用情况查询接口
 */
@Controller
@RequestMapping("/monitor")
public class MonitorController
{
	@Autowired
	private MonitorService monitorService;

	@RequestMapping("/getServerResourceByParams")
    @ResponseBody
    public MessageBean getServerResourceByParams(HttpServletRequest request)
	{
        MessageBean messageBean = new MessageBean();
        String mode = request.getParameter("mode");
        if(StringUtils.isNotBlank(mode))
        {
        	switch (mode)
    		{
    			case "top"://top模式时获取所有服务器资源占比排序列表
    				getDeviceOfComputerByTop(messageBean);
    				break;
    			
    			case "cpu":
    				getDeviceByCpu(messageBean);
    				break;
    				
    			case "all":
    				getDeviceAllStatus(messageBean);
    				break;
    			
    			default:
    				break;
    		}
        }else {
        	messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
        	messageBean.setMessage("mode参数未定义");
		}
        return messageBean;
    }
	
	private void getDeviceAllStatus(MessageBean messageBean) {
		
		List<Map<String, Object>> resultMap = monitorService.getDeviceAllStatus(); //获取设备状态信息
		
		if(resultMap == null || resultMap.size() <= 0)
		{
			messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			messageBean.setMessage("查询结果为空");
		}else {
			messageBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			messageBean.setData(resultMap);
			messageBean.setTotal(resultMap.size());
		}
		
	}

	@RequestMapping("/getDeviceOfSwitch")
    @ResponseBody
    public MessageBean getDeviceOfSwitch(HttpServletRequest request)
	{
        MessageBean messageBean = new MessageBean();
        Map<String, DeviceOfSwitch> resultMap = monitorService.getDeviceOfSwitch();
        if(resultMap != null && !resultMap.isEmpty())
        {
        	messageBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        	messageBean.setData(resultMap);
        	messageBean.setTotal(resultMap.entrySet().size());
        }else {
        	messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			messageBean.setMessage("查询结果为空");
		}
        return messageBean;
    }
	
	///top模式时获取所有服务器资源排序
	private void getDeviceOfComputerByTop(MessageBean messageBean)
	{
		Map<String, List<Map<String, String>>> resultMap = monitorService.getDeviceOfComputerByTop(3); //默认取前三
		if(resultMap == null || resultMap.entrySet().size() <= 0)
		{
			messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			messageBean.setMessage("查询结果为空");
		}else {
			messageBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			//messageBean.setData(resultMap);
			//适应前端接口，转换结果集
			convertTopResultMapToFrontInterface(messageBean, resultMap);
			messageBean.setTotal(resultMap.entrySet().size());
		}
	}
	
	//适应前端接口，将结果集转换为老版本接口结果集
	private void convertTopResultMapToFrontInterface(MessageBean messageBean,Map<String, List<Map<String, String>>> resultMap)
	{
		List<Map> list=new ArrayList<>();
		Map<String,Map> map=new HashMap<>();
        map.put("CPU TOP3", getTopMapValueFromList(resultMap.get("CPU")));
        map.put("内存 TOP3",getTopMapValueFromList(resultMap.get("MEMORY")));
        map.put("磁盘 TOP3",getTopMapValueFromList(resultMap.get("DISK")));
        map.put("温度 TOP3",getTopMapValueFromList(resultMap.get("FLOW")));
        for (Entry<String, Map> entry : map.entrySet())
		{
        	Map<String,Object> item=  new HashMap<String, Object>();
        	item.put("name", entry.getKey());
        	item.put("value", entry.getValue());
        	list.add(item);
		}
        messageBean.setData(list);
	}
	
	//适应前端接口，将list转换为有序MAP
	private Map<String, String> getTopMapValueFromList(List<Map<String, String>> list)
	{
		Map<String, String> result = new LinkedHashMap<String, String>();
		for (Map<String, String> map : list)
		{
			for (Entry<String, String> entry : map.entrySet())
			{
				result.put(entry.getKey(), entry.getValue());
			}
		}
		return result;
	}
	
	//获取服务器CPU使用率列表
	private void getDeviceByCpu(MessageBean messageBean)
	{
		List<Map<String, String>> resultList = monitorService.getDeviceByCpu();
		if (CollectionUtils.isEmpty(resultList))
		{
			messageBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
			messageBean.setMessage("查询结果为空");
		}else {
			messageBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			convertCpuResultToFrontInterface(messageBean, resultList);
			messageBean.setTotal(resultList.size());
		}
	}
	
	//适应前端接口，将CPU使用率结果集转换为老版本接口结果集
	private void convertCpuResultToFrontInterface(MessageBean messageBean,List<Map<String, String>> resultList)
	{
		Map<String, List> resultMap = new HashMap<>();
		List<Integer> allList = new ArrayList<>();
		List<String> nameList = new ArrayList<>();
		List<String> valueList = new ArrayList<>();
		for (Map<String, String> map : resultList)
		{
			for (Entry<String, String> entry : map.entrySet())
			{
				allList.add(100);
				nameList.add(entry.getKey());
				valueList.add(entry.getValue());
			}
		}
		resultMap.put("all", allList);
		resultMap.put("name", nameList);
		resultMap.put("value", valueList);
		messageBean.setData(resultMap);
	}
}
